<template>
  <div class="home">
    <div class="title">
      <van-icon @click="goback" size="25" class="iconfont" name="arrow-left" />
      <span>{{ title }}</span>
      <van-icon
        @click="goShopping"
        size="20"
        class="iconfont"
        name="shopping-cart-o"
      />
    </div>
    <div v-if="countdown" class="successful-payment">
      <div class="countdown">
        <span class="text">等待卖家付款</span>
        <span class="time"
          ><van-count-down
            millisecond
            :time="countdown"
            format="HH:mm:ss:SS"
          />后自动取消订单</span
        >
      </div>
      <div @click="cancel" class="btn-no">取消订单</div>
    </div>
    <div @click="showPopup" class="address">
      <van-icon size="20" name="location" />
      <div v-show="!showAddress">请填写收货地址</div>
      <div class="addressInfo" v-show="showAddress">
        <span>{{ user.name }}</span
        ><span>{{ user.tel }}</span
        ><br />
        <span
          >{{ user?.address?.province }}-{{ user?.address?.city }}-{{
            user?.address?.area
          }}-{{ user?.detailAddress }}</span
        >
      </div>
      <van-icon size="20" name="arrow" />
    </div>
    <van-popup
      v-model="showAddressProps"
      position="bottom"
      :style="{ height: '85%' }"
    >
      <van-area
        :columns-placeholder="['请选择', '请选择', '请选择']"
        @change="sureAddress"
        confirm-button-text=" "
        cancel-button-text=" "
        item-height="25"
        title="收货地址"
        :area-list="areaList"
      />
      <div class="van-contact-edit">
        <div class="van-contact-edit__fields">
          <div class="van-cell van-field">
            <div class="van-cell__title van-field__label">
              <span>详细地址</span>
            </div>
            <div class="van-cell__value van-field__value">
              <div class="van-field__body">
                <input
                  type="text"
                  v-model="detailAddress"
                  placeholder="请填写详细地址"
                  class="van-field__control"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <van-contact-edit
        is-edit
        show-set-default
        :contact-info="editingContact"
        set-default-label="设为默认地址"
        @save="onSave"
        @delete="onDelete"
      />
    </van-popup>
    <div style="height: 8px; width: 100%; background-color: #f6f6f6"></div>
    <div class="shop" v-for="(item, index) in goodsArr" :key="index">
      <div class="shop-title">
        <div>
          <div><van-icon size="16" name="shop-o" />{{ item.shop }}</div>
        </div>
        <div class="service">
          <van-icon size="16" color="#f45b59" name="chat-o" />联系商家
        </div>
      </div>
      <div class="goods" v-for="(good, i) in item.goods" :key="i">
        <van-image width="72" height="96" :src="good.image?.image" />
        <div class="desc">
          <div class="d-text">
            <span class="title">{{ good.title }}</span>
            <span class="text" v-if="good.chooseDetailArr.size"
              >尺码：{{ good.chooseDetailArr.size }}
            </span>
            <span class="text" v-if="good.chooseDetailArr.color"
              >颜色：{{ good.chooseDetailArr.color }}</span
            >
          </div>
          <div class="d-price">
            <span>{{ good.price }}</span>
            <span
              >x{{ good.num }}<van-icon size="17" color="#666" name="edit"
            /></span>
          </div>
        </div>
      </div>
    </div>
    <div class="accident">
      <div class="renew">
        <span>意外换新</span>
        <span class="desc"
          >衣服出现意外破损可以换新<van-icon
            size="14"
            color="#ff6f89"
            name="question"
        /></span>
        <span class="renew-p"
          >￥2.50
          <van-icon
            @click="select"
            v-show="showSelect"
            size="14"
            name="circle"
            color="#000"
          />
          <van-icon
            @click="select"
            v-show="!showSelect"
            size="14"
            name="checked"
            color="#ff4466"
          />
        </span>
      </div>
      <van-cell title="退货补运费" is-link value="商家赠送" />
      <van-cell title="延误必赔" is-link value="商家赠送" />
      <div class="msg">
        <span>订单备注</span>
        <div v-show="showMsg" class="tips">
          有什么想对商家说的可以写在这里哦~
        </div>
        <input
          @focus="userInput"
          @focusout="userInput"
          type="text"
          v-model="msg"
        />
      </div>
    </div>
    <div class="submit">
      <div class="total">
        <span>总价：</span>
        <span style="font-size: 12px; color: #ff4466">￥</span>
        <span style="font-size: 19px; color: #ff4466">{{ total }}</span>
      </div>
      <div style="font-size: 12px; color: #999">共{{ num }}件商品</div>
      <div @click="submit" class="buy">提交订单</div>
    </div>
    <van-popup
      closeable
      v-model="showSubmit"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <!-- <div class="submit"> -->
      <div class="p-title">收银台</div>
      <van-cell title="支付方式" is-link value="支付宝" />
      <van-cell title="支付金额" value-class="total-p" :value="`${total}元`" />
      <div @click="surePay" class="surePay">确认支付</div>
      <van-popup
        class="popups"
        position="bottom"
        :style="{ height: '60%' }"
        v-model="showPassword"
      >
        <!-- 密码输入框 -->
        <van-password-input
          info="请输入支付密码"
          :error-info="errorInfo"
          :value="password"
          :focused="showKeyboard"
          @focus="showKeyboard = true"
        />
        <!-- 数字键盘 -->
        <van-number-keyboard
          v-model="password"
          :show="showKeyboard"
          @blur="showKeyboard = false"
        />
      </van-popup>
      <!-- </div> -->
    </van-popup>
  </div>
</template>

<script>
import { areaList } from "@vant/area-data";
// import {mapState, mapMutations} from "vuex";
import { mapMutations, mapGetters } from "vuex";
export default {
  name: "OrderDetail",

  data() {
    return {
      title: "确认订单",
      showSelect: true,
      showMsg: true,
      showAddressProps: false,
      showAddress: false,
      showPassword: false,
      showKeyboard: false,
      showSubmit: false,
      areaList: areaList, //地区选择数据
      // provincesAndMunicipalities: [],
      detailAddress: "",
      editingContact: {},
      address: {},
      msg: "",
      total: 0, //总金额
      num: 0, //件数
      user: {},
      password: "",
      errorInfo: "", //密码错误信息
    };
  },

  computed: {
    goodsArr() {
      if (this.getLocalStroge("order")?.goodsArr) {
        return this.getLocalStroge("order")?.goodsArr;
      }
      return JSON.parse(this.$route.query.goodsArr);
    },
    // ...mapState({
    // 	orderActive: (state) => state.order.orders,
    // }),
    ...mapGetters("order", ["countdown"]),
  },
  created() {
    this.setOrders(this.getLocalStroge("order"));
  },
  mounted() {
    // let goods = JSON.parse(this.$route.query.goodsArr);
    this.goodsArr.map((item) => {
      item.goods.map((it) => {
        this.total += parseFloat(it.price.substring(1) * it.num);
        this.num += parseFloat(it.num);
      });
    });
    this.userInfo();
    console.log("goodsArr", this.goodsArr);
  },

  methods: {
    goback() {
      this.$router.go(-1);
    },
    goShopping() {
      this.$router.push({ name: "shoppingCar" });
    },
    select() {
      this.showSelect = !this.showSelect;
      if (!this.showSelect) {
        this.total += 2.5;
      } else {
        this.total -= 2.5;
      }
    },
    userInput() {
      if (this.msg) {
        this.showMsg = false;
      } else {
        this.showMsg = !this.showMsg;
      }
    },
    showPopup() {
      this.showAddressProps = !this.showAddressProps;
    },
    sureAddress(picker) {
      // this.address = `${picker.getValues()[0]?.name}-${picker.getValues()[1]?.name}-${picker.getValues()[2]?.name}`;
      this.address = {
        province: picker.getValues()[0]?.name,
        city: picker.getValues()[1]?.name,
        area: picker.getValues()[2]?.name,
      };
    },
    onSave(contactInfo) {
      let params = {
        name: contactInfo.name,
        tel: contactInfo.tel,
        address: this.address,
        detailAddress: this.detailAddress,
      };
      console.log(params);
      this.setLocalStroge("address", params);
      console.log("获取地址", this.getLocalStroge("address"));
      this.showPopup();
      this.userInfo();
    },
    onDelete() {
      this.delLocalStroge("address");
      this.showPopup();
      this.userInfo();
      this.$toast("删除成功");
    },
    userInfo() {
      let user = this.getLocalStroge("address");
      if (user) {
        this.showAddress = true;
        this.user = user;
      } else {
        this.showAddress = false;
      }
    },
    submit() {
      console.log(this.getLocalStroge("address"));

      if (this.getLocalStroge("address")) {
        this.showSubmit = !this.showSubmit;
        let orders = {
          goodsArr: this.goodsArr,
          created: new Date().getTime(),
        };
        this.setLocalStroge("order", orders);
        this.setOrders(orders);
      } else {
        this.$toast("请输入订单信息");
      }
    },
    cancel() {
      this.setOrders(0);
      this.delLocalStroge("order");
    },
    surePay() {
      this.showPassword = !this.showPassword;
    },
    ...mapMutations("order", ["setOrders"]),
  },
  watch: {
    password(password) {
      if (password.length === 6) {
        if (password === "123456") {
          this.setOrders(0);
          this.delLocalStroge("order");
          this.$toast.loading({
            message: "支付成功",
            forbidClick: true,
            duration: 300,
          });
          let orders = {
            goodsArr: this.goodsArr,
            success: new Date().getTime(),
          };
          // let success = [this.getLocalStroge("success", orders), orders];
          if (localStorage.getItem("success")) {
            console.log(1, orders);
            this.setLocalStroge("success", [orders,...this.getLocalStroge('success')]);
          } else {
            this.setLocalStroge("success", [orders]);
          }
          setTimeout(() => this.$router.push({ name: "my" }), 300);
        } else {
          this.$toast("密码错误!");
          this.password = "";
        }
      }
    },
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      if (vm.countdown) {
        vm.$toast("当前存在未支付的订单，请前往订单页面支付");
        if (from.path == "/order") {
          next();
        } else {
          vm.$router.go(-1);
        }
      } else {
        next();
      }
    });
  },
};
</script>

<style lang="scss" scoped>
.home {
  background-color: #fff;
  position: relative;
}
.title {
  width: 100%;
  margin: 0 auto;
  text-align: center;
  height: 44px;
  line-height: 44px;
  border-bottom: 1px solid #f4f4f4;
  z-index: 999;
  background-color: #fff;
  display: flex;
  justify-content: space-around;
  span {
    display: block;
    width: 70%;
  }
  .iconfont {
    font-size: 25px;
    line-height: 44px;
  }

  span {
    font-size: 18px;
  }
}
.shop {
  margin-top: 10px;
  padding: 15px;
  .shop-title {
    display: flex;
    justify-content: space-between;
    line-height: 20px;
    font-weight: 400;
    color: #666;
    font-size: 15px;
    align-items: center;
  }
  .goods {
    display: flex;
    justify-content: start;
    margin-top: 15px;
    .desc {
      display: flex;
      margin-left: 15px;
      justify-content: space-around;
      // align-items: center;
      .d-text {
        display: flex;
        flex-direction: column;
        justify-content: center;
        .title {
          width: 182px;
          font-size: 12px;
          font-weight: 700;
          color: #333;
          line-height: 17px;
          max-height: 34px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2; // 行数
          -webkit-box-orient: vertical;
        }
        .text {
          color: #ababab;
          font-size: 12px;
          width: 182px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2; // 行数
          -webkit-box-orient: vertical;
        }
      }
      .d-price {
        display: flex;
        flex-direction: column;
        justify-content: start;
        margin-left: 30px;
        margin-top: 7px;
      }
    }
  }
}
.renew {
  font-size: 13.5px;
  color: #333;
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  .desc {
    color: #999;
    font-size: 13px;
  }
  .renew-p {
    color: #f46;
  }
}
.msg {
  display: flex;
  position: relative;
  padding: 10px 15px;
  align-items: center;
  font-size: 13.5px;
  color: #333;
  .tips {
    position: absolute;
    font-size: 12px;
    color: #cfc6c6;
    left: 89px;
  }
  input {
    flex: 1;
    height: 31px;
    padding: 7.28px 10px;
    margin-left: 5px;
    background: #f7f7f7;
    border: none;
    border-radius: 5px;
    font-size: 12px;
    box-sizing: border-box;
  }
}
.submit {
  position: fixed;
  bottom: 0;
  width: 100%;
  display: flex;
  height: 52px;
  line-height: 52px;
  background-color: #fff;
  font-size: 15px;
  color: #333;
  .total {
    flex: 1;
  }
  .buy {
    background-image: linear-gradient(90deg, #f46, #ff468f);
    color: #fff;
    font-size: 15px;
    width: 104px;
    height: 52px;
    line-height: 52px;
    text-align: center;
    margin-left: 10px;
  }
}
.address {
  position: relative;
  min-height: 42px;
  line-height: 17.6px;
  padding: 10px 15px;
  // margin: 0 0 10px;
  font-size: 14.5px;
  color: #333;
  background: #fff url("/public/img/bg_address.png") repeat-x;
  background-size: 6%;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  .addressInfo {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3; // 行数
    -webkit-box-orient: vertical;
  }
  .van-icon-arrow {
    flex: 1;
    text-align: right;
  }
}
// .submit {
.p-title {
  height: 49px;
  line-height: 49px;
  text-align: center;
}
.popups {
  background-color: transparent;
}
.total-p {
  color: #000000;
  font-size: 26px;
}
.surePay {
  line-height: 41px;
  text-align: center;
  background-color: #ff5777;
  color: #ffffff;
  font-size: 18.72px;
  height: 41.6px;
  border: none;
  margin: 140px 15.6px 15.6px;
}
.successful-payment {
  background-image: linear-gradient(50deg, #f46, #ff468f);
  // width: 100%;
  height: 63.44px;
  padding: 0 15.6px 0 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .countdown {
    display: flex;
    flex-direction: column;
    .text {
      color: #fff;
      font-size: 19px;
      font-weight: 700;
    }
    .time {
      color: #fff;
      line-height: 17px;
      margin-top: 4px;
      font-size: 13px;
      display: flex;
      .van-count-down {
        color: #fff;
      }
    }
  }
  .btn-no {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    font-size: 13px;
    padding: 0 0.12rem;
    border: 1px solid #fff;
    border-radius: 7px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    color: #fff;
    vertical-align: middle;
    text-align: center;
  }
}
// }
::v-deep .van-contact-edit {
  padding: 0 16px;
}
::v-deep .van-button--danger {
  background-image: linear-gradient(90deg, #f46, #ff468f);
}
</style>
